package com.dodoke.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.dodoke.dto.RetailAreaDTO;
import com.dodoke.entity.RetailArea;
import com.dodoke.entity.RetailStore;
import com.dodoke.service.IRetailAreaService;
import com.dodoke.utils.AjaxResult;
import com.dodoke.utils.HttpStatus;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * 经销区域请求接口
 */
@Slf4j
@RestController
@RequestMapping("/retail/area")
public class RetailAreaController {
    @Autowired
    private IRetailAreaService retailAreaService;

    /**
     * 经销区域分页请求接口
     *
     * @param currentPage    当前页码
     * @param pageSize       每页显示数量
     * @param retailAreaDTO 模糊查询条件
     * @return
     */
    @GetMapping("/{currentPage}/{pageSize}")
    public AjaxResult getListByPage(@PathVariable Integer currentPage, @PathVariable Integer pageSize, RetailAreaDTO retailAreaDTO) {
        return AjaxResult.success("经销区域列表请求成功", retailAreaService.getListByPage(currentPage, pageSize, retailAreaDTO));
    }

    /**
     * 新增经销区域
     *
     * @param retailArea
     * @return
     */
    @PostMapping
    public AjaxResult addRetailArea(@Validated @RequestBody RetailArea retailArea) {
        // 判断经销区域名称是否存在
        RetailArea retailAreaByName = retailAreaService.getOne(new LambdaQueryWrapper<RetailArea>().eq(RetailArea::getName, retailArea.getName()));
        if (retailAreaByName != null) {
            return AjaxResult.error(HttpStatus.ERROR, "经销区域名称已存在");
        }
        // 判断经销区域编号是否存在
        RetailArea retailAreaByCode = retailAreaService.getOne(new LambdaQueryWrapper<RetailArea>().eq(RetailArea::getNo, retailArea.getNo()));
        if (retailAreaByCode != null) {
            return AjaxResult.error(HttpStatus.ERROR, "经销区域编号已存在");
        }
        int count = retailAreaService.saveRetailArea(retailArea);
        if (count <= 0) {
            return AjaxResult.error(HttpStatus.ERROR, "经销区域添加失败");
        }
        return AjaxResult.success("新增经销区域成功");
    }
}
